#include "Menu.h"
#include "../SGD Wrappers/SGD_GraphicsManager.h"
#include "../SGD Wrappers/SGD_InputManager.h"
#include "../SGD Wrappers/SGD_Event.h"
#include "../TinyXML/tinyxml.h"
#include "Game.h"
#include "BitmapFont.h"

/*MenuButton methods*/
MenuButton::MenuButton(SGD::Point pP)
{
	parentPos = pP;
}

void MenuButton::Update(float elapsedTime)
{
	
}

void MenuButton::Render()
{
	if (eventID != "null")
		SGD::GraphicsManager::GetInstance()->DrawRectangle(GetRect(), SGD::Color{ 0, 255, 0 });
	if (data != "null")
	{
		Game::GetInstance()->GetFont()->Draw(data.c_str(), 
			(int)((Game::GetInstance()->GetScreenWidth() / 2) - parentPos.x
			+ (dataPos.x - 100 - parentPos.x)), (int)dataPos.y, 1.f, SGD::Color(255, 225, 225));
	}
}

const SGD::Rectangle MenuButton::GetRect()
{
	return SGD::Rectangle{ { (Game::GetInstance()->GetScreenWidth() / 2) - parentPos.x 
		+ (position.x - 100 - parentPos.x), position.y }, size };
}

void MenuButton::Terminate()
{
	SGD::GraphicsManager::GetInstance()->UnloadTexture(image);
}

/*MenuItem methods*/
void MenuItem::Update(float elapsedTime)
{
	for (unsigned int i = 0; i < buttons.size(); i++)
		buttons[i].Update(elapsedTime);
}

void MenuItem::Render()
{
	BitmapFont* font = Game::GetInstance()->GetFont();
	font->Draw(itemName.c_str(), int((Game::GetInstance()->GetScreenWidth() / 2) - position.x - 100), 
		(int)position.y, 1, SGD::Color{ 255, 255, 255 });
	for (unsigned int i = 0; i < buttons.size(); i++)
		buttons[i].Render();
}

void MenuItem::Terminate()
{

}

/*Menu methods*/
void Menu::Initialize(std::string file)
{
	SGD::GraphicsManager* graphics = SGD::GraphicsManager::GetInstance();
	TiXmlDocument doc;
	doc.LoadFile(file.c_str());
	TiXmlElement* root = doc.RootElement();
	TiXmlElement* mItems = root->FirstChildElement();
	int numMenuItems;
	mItems->Attribute("count", &numMenuItems);
	TiXmlElement* menuItem = mItems->FirstChildElement();
	for (int items = 0; items < numMenuItems; items++)
	{
		MenuItem m;
		int x, y;
		menuItem->Attribute("posX", &x);
		menuItem->Attribute("posY", &y);
		const char* n = menuItem->Attribute("name");
		std::string name = n;
		m.SetItemName(name);
		m.SetPosition({ (float)x, (float)y });
		TiXmlElement* mButtons = menuItem->FirstChildElement();
		int numMenuButtons;
		mButtons->Attribute("count", &numMenuButtons);
		TiXmlElement* mButton = mButtons->FirstChildElement();
		for (int buttons = 0; buttons < numMenuButtons; buttons++)
		{
			MenuButton b(m.position);
			int x, y, w, h, dPX, dPY;
			mButton->Attribute("posX", &x);
			mButton->Attribute("posY", &y);
			mButton->Attribute("width", &w);
			mButton->Attribute("height", &h);
			mButton->Attribute("dPosX", &dPX);
			mButton->Attribute("dPosY", &dPY);
			const char* e = mButton->Attribute("eventID");
			const char* d = mButton->Attribute("data");
			const char* f = mButton->Attribute("image");
			std::string eID = e;
			std::string data = d;
			std::string fileP = f;
			b.SetEventID(eID);
			b.SetData(data);
			b.SetPosition({ (float)x, (float)y });
			b.SetSize({ (float)w, (float)h });
			b.SetDataPos({ (float)dPX, (float)dPY });
			if (fileP != "null")
				b.image = graphics->LoadTexture(fileP.c_str());
			m.buttons.push_back(b);
			mButton = mButton->NextSiblingElement();
		}
		menuItems.push_back(m);
		menuItem = menuItem->NextSiblingElement();
	}
}

void Menu::Update(float elapsedTime)
{
	for (unsigned int i = 0; i < menuItems.size(); i++)
		menuItems[i].Update(elapsedTime);
}

void Menu::Render()
{
	for (unsigned int i = 0; i < menuItems.size(); i++)
		menuItems[i].Render();
}

void Menu::Terminate()
{

}